const axios = require('axios');

function getData(url) {
    axios.get(url)
        .then(function (response) {
            // console.log(response.data);
            let dataUrl = '';
            dataUrl = getUrl(response.data);
            axios.get(dataUrl)
                .then(function (res) {
                    university = getUniversityTitle(res.data['data'][0]['title']);
                    score = res.data['data'][0]['score'];
                    rank = res.data['data'][0]['rank_display'];
                    console.log(university, score, rank);
                });
            // return response.data;
        });
}

let url = 'https://www.qschina.cn/university-rankings/university-subject-rankings/2020/communication-media-studies';

// console.log(getData(urSl));


// 获得请求地址
function getUrl(str) {
    const regex = /rank_url.*?(\d+\.txt)/m;
    // https://www.qschina.cn/sites/default/files/qs-rankings-data/cn/2005378.txt
    let m;
    let baseUrl = 'https://www.qschina.cn/sites/default/files/qs-rankings-data/cn/';

    if ((m = regex.exec(str)) !== null) {
        console.log(baseUrl + m[1]);
    }
    return baseUrl + m[1];
}

// 获取大学名称

function getUniversityTitle(str) {
    const regex = /<div.*?link">(.*?)<\/a><\/div>/;
    let m;
    if ((m = regex.exec(str)) !== null) {
        console.log(m[1]);
    }
    return m[1];
}



getData(url);
